package caidan.data;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.DefaultComboBoxModel;

public class Material
{
	public static final Material unChooseMaterial = new Material();
	public static ArrayList<Material> allMaterials = new ArrayList<Material>();

	static final MaterialData porkData = new MaterialData("猪肉",
			new String[]{"猪肉", "排骨", "猪蹄", "猪肚", "五花肉", "猪肝", "猪血", "猪腰", "猪皮",
					"猪肘", "猪耳朵", "猪心", "猪肺", "猪大肠", "猪大骨头", "猪小排", "猪里脊肉",
					"猪脑"});
	static final MaterialData beefData = new MaterialData("牛肉", new String[]{
			"牛肉", "牛腩", "牛肚", "牛肺", "牛排", "牛蹄筋", "牛肾", "牛尾", "肥牛"});
	static final MaterialData muttonData = new MaterialData("羊肉",
			new String[]{"羊肉", "羊肚", "羊血", "羊排", "羊蝎子", "羊骨", "羊肝", "羊肾"});
	static final MaterialData chickenData = new MaterialData("鸡肉",
			new String[]{"鸡肉", "鸡腿", "鸡胗", "鸡翅", "鸡肝", "鸡爪", "鸡血"});
	static final MaterialData duckData = new MaterialData("鸭肉",
			new String[]{"鸭肉", "鸭腿", "鸭胗", "鸭翅", "鸭肝", "鸭掌", "鸭肠", "鸭血"});
	static final MaterialData mmeatData = new MaterialData("肉制品",
			new String[]{"腊肉", "咸肉", "午餐肉", "火腿", "肉松", "粟肉", "香肠", "培根"});

	static final MaterialData eggData = new MaterialData("蛋类",
			new String[]{"鸡蛋", "鸭蛋", "咸鸭蛋", "鹅蛋", "鸽子蛋", "鹌鹑蛋", "松花蛋"});
	static final MaterialData milkData = new MaterialData("乳制品",
			new String[]{"奶酪", "黄油", "奶油"});

	static final MaterialData freFishData = new MaterialData("淡水鱼",
			new String[]{"草鱼", "鲤鱼", "鲫鱼", "鲢鱼", "青鱼", "鲶鱼", "银鱼", "罗非鱼", "武昌鱼",
					"鳊鱼", "桂鱼", "鮰鱼", "乌鳢", "泥鳅", "黄鳝"});
	static final MaterialData salFishData = new MaterialData("咸水鱼",
			new String[]{"带鱼", "鲈鱼", "鲅鱼", "金枪鱼", "鲳鱼", "鳗鱼", "鱿鱼", "三文鱼",
					"龙利鱼", "梭鱼", "秋刀鱼", "比目鱼", "沙丁鱼", "鱈鱼"});
	static final MaterialData mFishData = new MaterialData("鱼制品",
			new String[]{"鱼丸", "鱼干", "鱼籽", "鱼头", "花胶"});

	static final MaterialData shrimpData = new MaterialData("虾", new String[]{
			"虾", "虾米", "虾皮", "海虾", "河虾", "龙虾", "小龙虾", "皮皮虾", "虾仁"});
	static final MaterialData crabData = new MaterialData("蟹",
			new String[]{"螃蟹", "河蟹", "梭子蟹"});
	static final MaterialData algaeData = new MaterialData("藻类",
			new String[]{"海带", "海藻", "紫菜", "发菜", "裙带菜"});

	static final MaterialData leafData = new MaterialData("叶花类",
			new String[]{"白菜", "小白菜", "油菜", "芹菜", "菠菜", "韭菜", "韭黄", "生菜", "茼蒿",
					"香菜", "卷心菜", "蒜薹", "苋菜", "芥菜", "茭白", "雪里蕻", "菜花", "西兰花",
					"黄花菜", "豌豆尖", "荠菜", "百合", "蕨菜", "橄榄菜", "菊花", "槐花", "紫甘蓝",
					"西芹", "茴香", "娃娃菜", "芥蓝", "苦菊", "空心菜", "油麦菜", "马兰头", "水芹菜",
					"芦荟", "芝麻菜", "红菜苔", "枸杞菜", "豌豆苗", "萝卜叶", "木耳菜", "藜蒿", "马齿苋",
					"圆生菜", "芥兰", "紫背天葵"});
	static final MaterialData rootData = new MaterialData("根茎类",
			new String[]{"土豆", "红薯", "紫薯", "芋头", "萝卜", "胡萝卜", "红萝卜", "竹笋", "魔芋",
					"山药", "藕", "牛蒡", "荸荠", "莴笋", "芦笋", "春笋", "冬笋", "菱角", "慈菇",
					"甜菜根", "荞头", "苤蓝"});
	static final MaterialData melonData = new MaterialData("瓜果类",
			new String[]{"西红柿", "豆角", "四季豆", "豇豆", "扁豆", "荷兰豆", "黄瓜", "冬瓜",
					"苦瓜", "南瓜", "丝瓜", "茄子", "圆茄子", "绿茄子", "辣椒", "青椒", "红椒",
					"彩椒", "笋瓜", "方瓜", "节瓜", "瓠瓜", "佛手瓜", "西葫芦", "葫芦", "油豆角",
					"秋葵"});
	static final MaterialData mushroomData = new MaterialData("菌菇类",
			new String[]{"蘑菇", "香菇", "金针菇", "干香菇", "银耳", "木耳", "平菇", "草菇", "口蘑",
					"猴头菇", "竹荪", "杏鲍菇", "茶树菇", "鸡腿菇", "蟹味菇", "牛肝菌", "元蘑", "榛蘑",
					"海鲜菇", "红菇", "松蘑", "虫草花"});
	static final MaterialData bulbData = new MaterialData("葱蒜类",
			new String[]{"大蒜", "大葱", "洋葱", "小葱", "葱白", "青蒜", "蒜苗"});

	static final MaterialData beansData = new MaterialData("豆种子",
			new String[]{"花生", "黄豆", "毛豆", "青豆", "绿豆", "黑豆", "蚕豆", "豌豆粒", "鲜豌豆",
					"芸豆", "红腰豆", "白豆", "赤小豆", "花豆", "白扁豆", "山药豆", "鹰嘴豆"});
	static final MaterialData mBeansData = new MaterialData("豆制品", new String[]{
			"豆腐", "豆腐干", "腐竹", "千张", "豆腐丝", "豆腐皮", "豆豉", "黄豆芽", "绿豆芽"});

	static final MaterialData fruitData = new MaterialData("水果",
			new String[]{"苹果", "香蕉", "柠檬", "菠萝", "草莓", "山楂", "梨", "李子", "猕猴桃",
					"柚子", "芒果", "柿子", "荔枝", "石榴", "葡萄", "樱桃", "西瓜", "木瓜", "火龙果",
					"椰子", "无花果", "桂圆", "西瓜皮", "桑葚", "桃", "蔓越莓", "香瓜", "金桔",
					"杨梅", "蓝莓", "牛油果", "桔子", "杨桃", "百香果", "哈密瓜", "榴莲", "甘蔗",
					"佛手柑", "柑桔", "樱桃番茄", "橙", "鸭梨", "雪莲果", "覆盆子", "黑橄榄", "枇杷",
					"葡萄柚", "树莓", "香梨", "车厘子", "提子"});
	static final MaterialData dryFruitData = new MaterialData("干果",
			new String[]{"腰果", "松子", "核桃", "芝麻", "杏仁", "莲子", "榛子", "夏威夷果",
					"海底椰", "红枣", "南瓜子", "开心果", "板栗", "白果", "葵花籽仁", "桂圆肉", "山核桃",
					"葡萄干", "亚麻籽", "黑芝麻", "蜜枣", "柿饼"});

	static final MaterialData cerealsData = new MaterialData("五谷类",
			new String[]{"大米", "糯米", "黑米", "小米", "小麦", "麦芽", "玉米", "西米", "薏米",
					"燕麦", "红豆", "紫米", "糙米", "香米", "荞麦", "高粱米", "黄米", "大麦", "粳米",
					"鸭血糯", "红曲"});
	static final MaterialData riceData = new MaterialData("米面制品", new String[]{
			"荞麦面", "面包", "米饭", "粉丝", "米粉", "全麦粉", "小麦面粉", "燕麦片", "淀粉", "大米粥"});

	static final MaterialType noneType = new MaterialType((short) 0, "未选择");
	static final MaterialType meatType = new MaterialType((short) 1, "肉类",
			new MaterialData[]{porkData, beefData, muttonData, chickenData,
					duckData, mmeatData});
	static final MaterialType eggMilkType = new MaterialType((short) 2, "蛋/奶",
			new MaterialData[]{eggData, milkData});
	static final MaterialType fishType = new MaterialType((short) 3, "鱼类",
			new MaterialData[]{freFishData, salFishData, mFishData});
	static final MaterialType aquaticType = new MaterialType((short) 4, "水产",
			new MaterialData[]{shrimpData, crabData, algaeData});
	static final MaterialType vegetableType = new MaterialType((short) 5, "蔬菜",
			new MaterialData[]{leafData, rootData, melonData, mushroomData,
					bulbData});
	static final MaterialType beansType = new MaterialType((short) 6, "豆类",
			new MaterialData[]{beansData, mBeansData});
	static final MaterialType fruitType = new MaterialType((short) 7, "果品类",
			new MaterialData[]{fruitData, dryFruitData});
	static final MaterialType cerealsType = new MaterialType((short) 8, "五谷米面",
			new MaterialData[]{cerealsData, riceData});

	static final MaterialType[] allMaterialTypes = {noneType, meatType,
			eggMilkType, fishType, aquaticType, vegetableType, beansType,
			fruitType, cerealsType};

	private short type = -1;
	private short classify = -1;
	private short data = -1;
	private String name;
	private int totalQuality;

	Material()
	{
	}

	Material(short type, short classify, short data, String name)
	{
		this.type = type;
		this.classify = classify;
		this.data = data;
		this.name = name;
		allMaterials.add(this);
	}

	public short getType()
	{
		return type;
	}

	public short getClassify()
	{
		return classify;
	}

	public short getData()
	{
		return data;
	}

	public String getName()
	{
		return name;
	}

	public void addQuality(int quality)
	{
		totalQuality += quality;
	}

	public void resetQuality()
	{
		totalQuality = 0;
	}

	public int getQuality()
	{
		return totalQuality;
	}

	public MaterialType getMaterialType()
	{
		if (type <= 0 || type >= allMaterialTypes.length)
			return noneType;
		return allMaterialTypes[type];
	}

	public static Material getMaterial(short type, short classify, short data)
	{
		if (type <= 0 || type >= allMaterialTypes.length)
			return unChooseMaterial;
		return allMaterialTypes[type].getMaterial(classify, data);
	}

	public static Material getMaterial(int type, int classify, int data)
	{
		return getMaterial((short) type, (short) classify, (short) data);
	}

	void writeData(DataOutputStream dos) throws IOException
	{
		dos.writeShort(type);
		dos.writeShort(classify);
		dos.writeShort(data);
	}

	static Material readData(DataInputStream dis, int appVersion)
			throws IOException
	{
		return getMaterial(dis.readShort(), dis.readShort(), dis.readShort());
	}
}

class MaterialType extends MaterialData
{
	private short typeIndex;
	private MaterialData[] materialDatas;

	private Material[][] materialss;
	private Material[] materials;

	public MaterialType(short index, String name)
	{
		this.typeIndex = index;
		this.name = name;
	}

	public MaterialType(short index, String name, MaterialData[] materialDatas)
	{
		this.typeIndex = index;
		this.name = name;
		this.materialDatas = materialDatas;
		materialss = new Material[materialDatas.length][];
		for (short i = 0; i < materialDatas.length; i++)
		{
			String[] datas = materialDatas[i].getDatas();
			materialss[i] = new Material[datas.length];
			for (short j = 0; j < datas.length; j++)
				materialss[i][j] = new Material(index, i, j, datas[j]);
		}
	}

	public MaterialType(short index, String name, String[] datas)
	{
		this.typeIndex = index;
		this.name = name;
		this.datas = datas;
		materials = new Material[datas.length];
		short j = -1;
		for (short i = 0; i < datas.length; i++)
			materials[i] = new Material(index, i, j, datas[i]);
	}

	Material getMaterial(short classify, short data)
	{
		if (materialss != null)
		{
			if (classify < 0 || classify >= materialss.length)
				return Material.unChooseMaterial;
			if (data < 0 || data >= materialss[classify].length)
				return Material.unChooseMaterial;
			return materialss[classify][data];
		}
		if (classify < 0 || classify >= materials.length)
			return Material.unChooseMaterial;
		return materials[classify];
	}

	public short getTypeIndex()
	{
		return typeIndex;
	}

	@Override
	public DefaultComboBoxModel getComboBoxModel()
	{
		return new DefaultComboBoxModel(
				materialDatas != null ? materialDatas : datas);
	}

	public boolean isClassifyData()
	{
		return materialDatas != null;
	}

	public MaterialData getMaterialData(short index)
	{
		return materialDatas[index];
	}
}

class MaterialData
{
	protected String name;
	protected String[] datas;
	// protected DefaultComboBoxModel[] comboBoxModels = new
	// DefaultComboBoxModel[Material.modelNum];

	public MaterialData()
	{
	}

	public MaterialData(String name, String[] datas)
	{
		this.name = name;
		this.datas = datas;
	}

	public String getName()
	{
		return name;
	}

	public String[] getDatas()
	{
		return datas;
	}

	public DefaultComboBoxModel getComboBoxModel()
	{
		return new DefaultComboBoxModel(datas);
	}

	@Override
	public String toString()
	{
		return name;
	}
}
